private var Player:GameObject;
var speed = 3.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var rotationSpeed = 50.0;
var ratioSprint = 1.5;
private var moveDirection = Vector3.zero;
private var GamePaused = true;

function Start()
{
	Player = gameObject;
	Player.animation.Stop();
	var idle = Player.animation["Idle"];
	idle.wrapMode = WrapMode.Loop;
	
	var walk = Player.animation["Walk"];
	walk.wrapMode = WrapMode.Loop;
	
	var run = Player.animation["Run"];
	run.wrapMode = WrapMode.Loop;
	
	var jump = Player.animation["Jump"];
	jump.wrapMode = WrapMode.Clamp;
	
	idle.layer =- 1;
	run.layer =- 1;
	walk.layer =- 1;
	Player.animation.CrossFade("Idle");
	SendMessage("SyncAnimation", "Idle");
}

function FixedUpdate()
{
	if(!GamePaused){
		var controller:CharacterController = GetComponent(CharacterController);
		if(controller.isGrounded)
		{		
			moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= speed;
			
			if(Input.GetAxis("Vertical") && controller.isGrounded)
			{
				if(Input.GetKey(KeyCode.LeftShift))
				{
					moveDirection *= ratioSprint;
					Player.animation.CrossFade("Run");
					SendMessage("SyncAnimation", "Run");
				}
				else{
					Player.animation.CrossFade("Walk");
					SendMessage("SyncAnimation", "Walk");
				}
			}
			else
			{
				Player.animation.CrossFade("Idle");
				SendMessage("SyncAnimation", "Idle");
			}
			
			if(Input.GetButton("Jump"))
			{
				moveDirection.y = jumpSpeed;
				Player.animation.Rewind("Jump");
				Player.animation.CrossFade("Jump");
			}
			
		}
		moveDirection.y -= gravity * Time.deltaTime;
		controller.Move(moveDirection * Time.deltaTime);
	}
	else{
		Player.animation.CrossFade("Idle");
	}
}

function TogglePause(){
	GamePaused = !GamePaused;
}